﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net.NetworkInformation;
using System.Text;
using System.Windows.Forms;

namespace GBT.Client.FileOperation
{
    public static class FtpExtension
    {
        /// <summary>
        /// 设置ListViewItems
        /// </summary>
        /// <param name="listViewItems"></param>
        /// <param name="ftpListItem">ftp返回的列表</param>
        /// <param name="imageIndex">图标索引</param>
        /// <param name="tag">标签：文件夹或者文件</param>
        /// <param name="view">视图</param>
        public static void SetListViewItems(this List<ListViewItem> listViewItems, FtpListItem ftpListItem, int imageIndex, string tag, View view)
        {
            ListViewItem result = new ListViewItem();
            //switch (view)
            //{
            //    case View.Details:
                    result.Text = ftpListItem.Name;
                    result.Tag = tag;
                    if(ftpListItem.Name == "Recycled")
                    {
                        result.ImageIndex = 3;
                    }
                    else
                    {
                        result.ImageIndex = imageIndex;
                    }
                    result.SubItems.Add(ftpListItem.Type.ToString());
                    result.SubItems.Add(ftpListItem.Modified.ToString());
                    result.SubItems.Add(ftpListItem.Size.ToSizeString(tag));
                //    break;
                //case View.LargeIcon:
                //    result.Text = ftpListItem.Name;
                //    if (ftpListItem.Name == "Recycled")
                //    {
                //        result.ImageIndex = 3;
                //    }
                //    else
                //    {
                //        result.ImageIndex = imageIndex;
                //    }
                //    result.Tag = tag;
                //    break;
            //}
            listViewItems.Add(result);
        }

        public static string ToSizeString(this long number, string tag)
        {
            switch (tag)
            {
                case "directory":
                    return "";
                case "file":
                    return ((int)Math.Ceiling(number / (double)1024)).ToString() + " KB";
                default:
                    return "";
            }
        }

        /// <summary>
        /// 文件的扩展名
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string ToExtension(this string fileName)
        {
            return Path.GetExtension(fileName);
        }

        /// <summary>
        /// 获取文件夹名称
        /// </summary>
        /// <param name="fileFullPath"></param>
        /// <returns></returns>
        public static string ToDirectoryName(this string fileFullPath)
        {
            return Path.GetDirectoryName(fileFullPath);
        }

        /// <summary>
        /// Ping指定的主机，看能否ping通
        /// </summary>
        /// <param name="Address">(主机地址)</param>
        /// <param name="TimeOut">(超时时间,默认:1s)</param>
        /// <returns>True if a response is received, false otherwise</returns>
        public static bool PingHost(this string Address, int TimeOut = 1000)
        {
            try
            {
                using (Ping PingSender = new Ping())
                {
                    PingOptions Options = new PingOptions();
                    Options.DontFragment = true;
                    string Data = "test";
                    byte[] DataBuffer = Encoding.ASCII.GetBytes(Data);
                    PingReply Reply = PingSender.Send(Address, TimeOut, DataBuffer, Options);
                    if (Reply.Status == IPStatus.Success)
                        return true;
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 获取上级文件夹路径
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="degree">级数</param>
        /// <param name="root"></param>
        /// <returns></returns>
        public static string GetParentDirectory(this string path, string root)
        {
            string newPath;
            if (path.LastIndexOf("\\") > 0)
            {
                newPath = path.Substring(0, path.LastIndexOf('\\'));
            }
            else
            {
                newPath = "";
            }
            if (string.IsNullOrEmpty(newPath))
            {
                newPath = "\\" + root;
            }
            return newPath;
        }

        /// <summary>
        /// 获取当前目录名或文件名
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="degree">级数</param>
        /// <param name="root"></param>
        /// <returns></returns>
        public static string GetDirectoryNameOrFileName(this string path)
        {
            string name;
            if (path.LastIndexOf("\\") > 0)
            {
                int startIndex = path.LastIndexOf('\\');
                int length = path.Length - startIndex;
                name = path.Substring(startIndex, length);
            }
            else
            {
                name = "";
            }
            return name;
        }

        /// <summary>
        /// 判断是否是回收站
        /// </summary>
        /// <param name="remoteDir"></param>
        /// <returns></returns>
        public static bool IsRecycled(this string remoteDir)
        {
            return remoteDir.Contains("\\Recycled");
        }

        public static bool IsRecycled(this ListView listView)
        {
            foreach (ListViewItem listViewItem in listView.SelectedItems)
            {
                if (listViewItem.Text == "Recycled")
                {
                    return true;
                }
            }
            return false;
        }

        public static string GetObjectType(this string path)
        {
            string type = "";
            if (Directory.Exists(path))
            {
                type = "directory";
            }
            else if (File.Exists(path))
            {
                type = "file";
            }
            return type;
        }
    }
}
